<!DOCTYPE html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=yes">
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://shc7432.github.io/public/getLinkInput/getLinkInput.js"></script>
<title>在线代码编辑器</title>
<body style="width:90%;margin: 0 auto;text-align:center;">
<h1>在线代码编辑器</h1><div style="text-align:right;font-size:10px">编程语言:<select id=sct1><option>C</option><option>C#</option><option>C++</option><option>CSS</option><option id=sct1html value=html>HTML</option><option>Java</option><option id=sct1js value=js>JavaScript</option><option>JSON</option><option>PHP</option><option>Python</option><option>R</option><option>RD</option><option>SQL</option><option>XML</option></select></div>
<textarea cols=50 rows=10 placeholder="输入代码,自动执行" onkeyup="stathtml()" id=intxt></textarea>
<br>执行结果<br><iframe src="javascript:document.write('您还没有开始执行!')" style="border:1px solid;text-align:left;"></iframe>
<button id="resulttoother">分享结果</button><button id="copyinputcode">复制代码</button>
<div style="font-size:1px">友情链接:<a href="https://shc0743.github.io/MyFile/index1/demo/mdn/html/form/forms/code-test/mob/" style="text-decoration:none">本地代码编辑器</a>(若出错,请选择此项)</div><script>
tmp=document.getElementById("sct1").getElementsByTagName('option')
for(i=0;i<tmp.length;i++){tmp[i].disabled=1}
sct1html.disabled=0
sct1js.disabled=0
sct1html.selected=1
delete tmp;delete a;
function stathtml(){
document.getElementsByTagName('iframe')[0].src="test.html?type=html&code="+encodeURIComponent(intxt.value)
}
function statjs(){
try{
//if(intxt.value.search("alert()")!==-1||intxt.value.search("document.documentElement.innerHTML=")!==-1||intxt.value.search("document.body.innerHTML=")!==-1||intxt.value.search("document.write")!==-1){document.getElementsByTagName('iframe')[0].innerHTML="External error:The operation has been disabled by the administrator.";return;}
document.getElementsByTagName('iframe')[0].src="test.html?type=js&code="+encodeURIComponent(intxt.value)
}catch(err){document.getElementsByTagName('iframe')[0].innerHTML=err}
}
sct1.onchange=function(){
if(this.value=="html"){intxt.onkeyup=stathtml;intxt.onblur="";intxt.placeholder="输入代码,自动执行"} else 
if(this.value=="js"){intxt.onkeyup=statjs;intxt.onblur="";intxt.placeholder="输入代码,自动执行,支持jQuery哦"}
}

$(function(){
$("#resulttoother").button()
$("#copyinputcode").button()
})

document.querySelectorAll("#resulttoother")[0].onclick=function(){
copytext(document.getElementsByTagName('iframe')[0].src)
  }
document.querySelectorAll("#copyinputcode")[0].onclick=function(){
copytext(document.getElementsByTagName('textarea')[0].value)
  }

</script>
<div class="copytextsucce" title="复制成功">
<div style="font-size:16px;height:30px;">复制成功!</div>
</div>
<div class="copytextfail" title="复制失败">
<div style="border:1px solid #ccc;background-color:#aaa;">
请按 Ctrl + C 复制<br>
<textarea class="copyfailoutput" cols="40" rows="6" style="background-color:#fff;border:0px solid #ccc;"></textarea></div>
</div>
<script>
//原生js实现复制内容到剪切板，兼容pc、移动端（支持Safari浏览器）
$(function(){

$(".copytextsucce").dialog({
autoOpen: false,
modal: true,
width: 125,
height: "auto",
buttons: {
"确定": function(){
$(this).dialog("close");
}
 },
close: function(){
}
  })
$(".copytextfail").dialog({
autoOpen: false,
modal: true,
buttons: {
"确定": function(){
$(this).dialog("close");
}
 },
close: function(){
}
  })

} )
function copytext(text){
if(typeof document.execCommand!=="function"){
$(function(){
$(".copytextfail").dialog("open");
document.querySelectorAll(".copyfailoutput")[0].value=text;
})
return false;
}
var dom = document.createElement("textarea");
dom.value = text;
dom.setAttribute('style', 'display: block;width: 1px;height: 1px;');
document.body.appendChild(dom);
dom.select();
var result = document.execCommand('copy');
document.body.removeChild(dom);
if (result) {
$(function(){
$(".copytextsucce").dialog("open");
})
return true;
}
if(typeof document.createRange!=="function"){
$(function(){
$(".copytextfail").dialog("open");
document.querySelectorAll(".copyfailoutput")[0].value=text;
})
return false;
}
var range = document.createRange();
var div=document.createElement('div');
div.innerHTML=text;
div.setAttribute('style', 'height: 1px;fontSize: 1px;overflow: hidden;');
document.body.appendChild(div);
range.selectNode(div);
const selection = window.getSelection();
if (selection.rangeCount > 0){
selection.removeAllRanges();
}
selection.addRange(range);
document.execCommand('copy');
$(function(){
$(".copytextsucce").dialog("open");
})
return true;
}
</script>
